package mvc.helper
{
	import define.DBConst;

	import flash.data.SQLStatement;
	import flash.events.SQLEvent;

	import mx.events.FlexEvent;
	import mx.utils.StringUtil;

	public class BatchSqlHelper
	{
		public function BatchSqlHelper()
		{
			if(_instance != null)
				throw new Error("Singleton!");
			_instance = this;
		}
		// 实例
		private static var _instance:BatchSqlHelper;

		public static function getInstance():BatchSqlHelper
		{
			if(_instance == null)
				new BatchSqlHelper();
			return _instance;
		}
		// ↑↑↑ Singleton ↑↑↑
		private var _view:BatchSql;

		public function creationCompleteListener(event:FlexEvent):void
		{
			this._view = event.currentTarget as BatchSql;
		}

		public function btnRunClickListener():void
		{
			var sqls:Array = _view.txtInput.text.split(";");
			for each(var sql:String in sqls)
			{
				sql = StringUtil.trim(sql);
				if(sql == "")
					continue;
				var stmt:SQLStatement = new SQLStatement();
				stmt.sqlConnection = DBConst.armoryConn;
				stmt.text = sql;
				trace(sql);
				stmt.addEventListener(SQLEvent.RESULT, stmt_resultHandler);
				stmt.execute();
			}
			_view.txtInput.text = "";
		}

		private function stmt_resultHandler(event:SQLEvent):void
		{
			var data:Array = (event.target as SQLStatement).getResult().data;
			for each(var o:Object in data)
			{
				var oStr:String = "";
				for(var key:String in o)
				{
					oStr = oStr + key + " = " + o[key] + ", ";
				}
				trace(oStr);
			}

		}
	}
}